library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.3.2
## Warning: package 'forcats' was built under R version 4.3.2
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.2 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.2 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
#below code may be a bad practice
ggplot(data=faithful,
mapping=aes(x=eruptions,
y=waiting)) +
geom_point()
ggplot(data=faithful,
mapping=aes(x=eruptions,
y=waiting), shape='square') +
geom_point()
#Pro-practice
ggplot(data=faithful)+
geom_point(aes(x=eruptions,
y=waiting,
color=eruptions>3))
ggplot(data=mtcars)+
geom_point(aes(x=cyl,
y=disp,
color=cyl>4))
ggplot(data=mtcars)+
geom_point(aes(x=hp,
y=cyl,
color=hp<65))
ggplot(data=diamonds)+
geom_point(aes(x=color,
y=depth,
color=depth>70))
ggplot(data=faithful)+
geom_point(aes(x=eruptions,
y=waiting))+
geom_abline(slope=-40,intercept=200)
#Histogram-for histgrams we need only 1 continuous variable for mapping x=…
ggplot(faithful)+
geom_histogram(aes(x=eruptions))
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
ggplot(faithful)+
geom_histogram(aes(x=eruptions, color=eruptions<3.1))
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
ggplot(faithful)+
geom_histogram(aes(x=eruptions, fill=eruptions<3.1))
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
ggplot(faithful)+
geom_histogram(aes(x=eruptions, fill=waiting<60), position = 'identity',alpha=0.5)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
ggplot(faithful)+
geom_histogram(aes(x=eruptions, fill=waiting<60), position = 'dodge',alpha=0.5)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
ggplot(mtcars)+
geom_histogram(aes(x=cyl, fill=gear<4), position = 'dodge',alpha=0.5)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
ggplot(mtcars)+
geom_histogram(aes(x=hp))
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
ggplot(mtcars)+
geom_histogram(aes(x=cyl))
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
ggplot(faithful,
aes(x=eruptions,y=waiting))+
geom_density_2d()+
geom_point()
ggplot(faithful,
aes(x=eruptions,y=waiting))+
geom_density2d_filled()+
geom_point()
#Bar Plots
ggplot(mpg)+
geom_bar(aes(x=class))
ggplot(diamonds)+
geom_bar(aes(x=cut))
ggplot(diamonds)+
stat_count(aes(x=cut))
ggplot(mpg)+
stat_count(aes(x=manufacturer))
ggplot(mpg)+
stat_count(aes(x=class), geom='point'
)
ggplot(mpg)+
geom_jitter(aes(x=class, y=hwy), width=0.2)+
stat_summary(aes(x=class, y=hwy), fun=mean, geom='point', colour='red', size=4)
ggplot(mpg)+
geom_jitter(aes(x=class, y=hwy), width=0.2)+
stat_summary(aes(x=class, y=hwy), fun=median, geom='point', colour='red', size=4)
#Not a great plot
ggplot(mpg)+
geom_point(aes(x=displ, y=hwy, color=class))
ggplot(mpg)+
geom_point(aes(x=displ, y=hwy, color=class))+
scale_color_brewer(type='qual')
ggplot(mpg)+
geom_point(aes(x=displ, y=hwy, color=class))+
scale_color_brewer(name='CLASS_ravi', type='qual')
ggplot(mpg)+
geom_point(aes(x=displ, y=hwy, color=class))+
scale_x_continuous(breaks=c(3,5,6))+
scale_y_continuous(trans='log10')
ggplot(mpg)+
geom_point(aes(x=displ, y=hwy, color=class))+
scale_color_brewer(palette = 'Set1')
#buBBLE CHART
ggplot(mpg)+
geom_point(aes(x=displ, y=hwy, color=class, size=cyl))+
scale_color_brewer(type = 'qual')
#Now legend is correctly reflecting
ggplot(mpg)+
geom_point(aes(x=displ, y=hwy, color=class, size=cyl))+
scale_color_brewer(type = 'qual')+
scale_size(breaks=c(4,5,6,8))
ggplot(mpg)+
geom_point(aes(x=displ, y=hwy, color=class, size=cyl))+
guides(colour='legend')
#Interesting
ggplot(mpg)+
geom_point(aes(x=displ, y=hwy, color=cty, size=cty))+
guides(colour='legend')
ggplot(mpg)+
geom_point(aes(x=displ, y=hwy))+
facet_wrap(~class)
ggplot(mpg)+
geom_point(aes(x=displ, y=hwy))+
facet_grid(~drv)
ggplot(mpg)+
geom_point(aes(x=displ, y=hwy))+
facet_wrap(~drv, scales = 'free')
ggplot(mpg)+
geom_point(aes(x=displ, y=hwy))+
facet_wrap(~drv, scales = 'free_x')
ggplot(mpg)+
geom_point(aes(x=displ, y=hwy))+
facet_wrap(~drv+year)
ggplot(mpg)+
geom_bar(aes(x=class))+
scale_y_continuous(limits = c(0,40))
## Warning: Removed 3 rows containing missing values (`geom_bar()`).
library(tidyverse)
ggplot(mpg)+
geom_bar(aes(x=class))+
coord_cartesian(ylim = c(0,40))
#GGANIMATE-Story Telling and attention grabbing #1-Transitioning through time
# install.packages("gganimate")
library(gapminder)
## Warning: package 'gapminder' was built under R version 4.3.2
library(gganimate)
## Warning: package 'gganimate' was built under R version 4.3.2
library(gifski)
## Warning: package 'gifski' was built under R version 4.3.2
## standard ggplot2
myPlot <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, colour = country)) +
geom_point(alpha = 0.7, show.legend = FALSE) +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
scale_x_log10() +
# Here comes the gganimate specific bits
labs(title = 'Year: {frame_time}', x = 'GDP per capita', y = 'life expectancy') +
transition_time(year) +
ease_aes('linear')
animate(myPlot, duration = 10, fps = 30, width = 750, height = 750, renderer = gifski_renderer())
anim_save("output.gif")
library(tidyverse)
library(gganimate)
library(gapminder)
g <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, colour = country)) +
geom_point(alpha = 0.7, show.legend = FALSE) +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
scale_x_log10() +
facet_wrap(~continent) +
# Here comes the gganimate specific bits
labs(title = 'Year: {frame_time}', x = 'GDP per capita', y = 'life expectancy') +
transition_time(year) +
ease_aes('linear')
animate(g, height=400, width=600, renderer=gifski_renderer())
anim_save("gapminder.gif", g)
```{# {r} # amim_test$Year %>% # unique() # # scatter_anim <- amim_test %>% # ggplot(aes(x=Year, y=Sale # ))+ # geom_point(aes(color=Brand, # size=Sale))+ # ggrepel::geom_label_repel(aes(label=Sale), # nudge_x = -0.2, # alpha=0.7, # size=6)+ # theme_bw(base_size = 20) # # animate_scatter <- scatter_anim+ # labs(title = “Test”, # subtitle = “Year:{frame_time}”, # caption = “Test 2”)+ # transition_time(Year)+ # ease_aes(‘linear’) # animate(animate_scatter, # duration=9, # fps=1, # end_pause = 2, # width=1000, # height=800)
```